package com.jrx.anytxn.param.job.collection;

import com.jrx.anytxn.common.uitl.DateUtils;
import com.jrx.anytxn.param.config.FilePathsConfig;
import com.jrx.anytxn.param.constant.Constant;
import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.mapper.ext.ExtBtBatchStatusMapper;
import com.jrx.anytxn.param.mapper.ext.ExtPrOrganizationTableMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.io.File;


/**
 * 催收结束
 */
@Configuration
@EnableConfigurationProperties(FilePathsConfig.class)
public class CollectionEndConfiguration {
    private static final Logger logger = LoggerFactory.getLogger(CollectionEndConfiguration.class);


    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Resource
    private ExtPrOrganizationTableMapper extPrOrganizationTableMapper;
    @Resource
    private ExtBtBatchStatusMapper extBtBatchStatusMapper;
    @Autowired
    private FilePathsConfig filePathsConfig;

    @Bean
    public Job collectionEndJob() {
        return jobBuilderFactory.get("collectionEndJob")
                .start(collectionEndStep())
                .build();
    }

    @Bean
    public Step collectionEndStep() {
        return stepBuilderFactory.get("collectionEndStep")
                .tasklet(collectionEndTasklet())
                .build();
    }

    @Bean
    public Tasklet collectionEndTasklet() {
        return (stepContribution, chunkContext) -> {
            logger.info("催收批量结束");
            //获取业务日期
            PrOrganizationTable org =
                    extPrOrganizationTableMapper.selectByOrgIdForBatch(Constant.DEFAULT_ORG_NUMBER);

            String filePath = filePathsConfig.getCollectionFilePath();
            String toDay = DateUtils.format(org.getNextProcessingDate(),"yyyyMMdd");
            String collrctionPath = filePath + org.getTenantId() + File.separator + toDay + File.separator;
            String fileName = toDay + "_" + org.getTenantId() + ".ok";
            File okFile = new File(collrctionPath + fileName);
            if(!okFile.exists()) {
                okFile.createNewFile();//按照租户生成OK文件
            }
            //更新执行中的job为完成
            extBtBatchStatusMapper.updateByBusinessDateAndStatusForBatch(org.getNextProcessingDate(),
                    Constant.BATCH_SATTUS_1,Constant.BATCH_SATTUS_2,Constant.COLLECTION_JOB);
            return RepeatStatus.FINISHED;
        };
    }
}
